package com.xiaoyu.cert;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;

public class Certifi {
	public static Certificate generateCertificate(String path){
		File file = null;
		FileInputStream fis = null;
		Certificate certificate = null;
		try {
			file = new File(path);
			if(!file.exists()){
				file.createNewFile();
			}
			CertificateFactory certificateFactory = CertificateFactory.getInstance("x.509");
			fis = new FileInputStream(file);
			certificate = certificateFactory.generateCertificate(fis);
		} catch (CertificateException e) {
			e.printStackTrace();
		}catch (NullPointerException e) {
			System.out.println("文件名不能为空！");
		} catch (FileNotFoundException e) {
			System.out.println("没有找到对应文件！");
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			if(fis!=null){
				try {
					fis.close();
				} catch (IOException e) {
					System.out.println("文件关闭错误！");
				}
			}
		}
		return certificate;
	}
}
